//
//  FoodVideoViewController.m
//  FoodList
//
//  Created by 王金晶 on 16/4/6.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "FoodVideoViewController.h"
#import "VideoMainController.h"
#import "RadioMainController.h"
#import "VideoModel.h"
#import "Video+CoreDataProperties.h"
#import "AppDelegate.h"

@interface FoodVideoViewController ()

@property (nonatomic, strong) VideoMainController *videoVc;

@property (nonatomic, strong)NSManagedObjectContext *context; // 临时数据库


@end

@implementation FoodVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // 获取临时数据库
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    self.context = delegate.managedObjectContext;
    
    NSArray *titleArr = @[@"边看边学", @"边听边学"];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:titleArr];
    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segment;
    
    [self creatChildController];
    
    NSFetchRequest *requset = [[NSFetchRequest alloc] initWithEntityName:@"Video"];
    NSMutableArray *array = [[_context executeFetchRequest:requset error:nil] mutableCopy];
    if (array.count != 0) {
        self.videoVc.modelArray = array;
    }else {
        [self requestVideoData];
    }
    
}

- (void)requestVideoData {
    [RequestManger requestWithURL:@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=%E5%BC%80%E8%83%83" requestType:nil parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *videoListArr = dataDic[@"videoList"];
        NSMutableArray *modelArr = [NSMutableArray array];
        for (NSDictionary *dic in videoListArr) {
            VideoModel *model = [[VideoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *consumptionDic = dic[@"consumption"];
            [model setValuesForKeysWithDictionary:consumptionDic];
            model.myDescription = dic[@"description"];
            NSDictionary *dic = model.playInfo[1];
            NSString *urlStr = dic[@"url"];
            model.playUrl = urlStr;
            [modelArr addObject:model];
            Video *videoData = [NSEntityDescription insertNewObjectForEntityForName:@"Video" inManagedObjectContext:_context];
            videoData.title = model.title;
            videoData.duration = [NSNumber numberWithInt:model.duration];
            videoData.coverForSharing = model.coverForSharing;
            videoData.coverBlurred = model.coverBlurred;
            videoData.collectionCount = [NSNumber numberWithInteger:model.collectionCount];
            videoData.myDescription = model.myDescription;
            videoData.playCount = [NSNumber numberWithInteger:model.playCount];
            videoData.playUrl = model.playUrl;
            videoData.shareCount = [NSNumber numberWithInteger:model.shareCount];
            [_context save:nil];
        }
        _videoVc.modelArray = modelArr;
        dispatch_async(dispatch_get_main_queue(), ^{
            [_videoVc.videoTableView reloadData];
        });
        
    } error:^(NSError *error) {
        NSLog(@"fail");
    }];
}
// 创建子控制器
- (void)creatChildController {
    self.videoVc = [[VideoMainController alloc] initWithNibName:@"VideoMainController" bundle:[NSBundle mainBundle]];
    [self.view addSubview:_videoVc.view];
    [self addChildViewController:_videoVc];
    
    RadioMainController *radioVc = [[RadioMainController alloc] initWithNibName:@"RadioMainController" bundle:[NSBundle mainBundle]];
    [self.view addSubview:radioVc.view];
    radioVc.view.hidden = YES;
    [self addChildViewController:radioVc];
}
// segment 点击事件
- (void)segmentedAction:(UISegmentedControl *)segment {
    UIViewController *tempVc = self.childViewControllers[segment.selectedSegmentIndex];
//    NSLog(@"%@", [tempVc class]);
    for (UIViewController *viewVc in self.childViewControllers) {
        // 判断 我当前所遍历到的这个 controller 是不是属于 OneViewController这个类
        if ([viewVc isKindOfClass:[tempVc class]]) {
            viewVc.view.hidden = NO;
        }else {
            viewVc.view.hidden = YES;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
